//
//  SetCommunicationPasswordViewController.m
//  ALS3.0
//
//  Created by guomianxiang on 16/12/28.
//  Copyright © 2016年 guomianxiang. All rights reserved.
//

#import "SetCommunicationPasswordViewController.h"
//#import "LGtools.h"
//#import "ALSConstNSString.h"
#import "ALSHeader.h"
@interface SetCommunicationPasswordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *setCommunicationPasswordView;
@property (weak, nonatomic) IBOutlet UITextField *communicationPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *ascertainBut;
@property (weak, nonatomic) IBOutlet UIButton *cancelCommunicationPasswordBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *setCommunicationPasswordViewTop;
@property (weak, nonatomic) IBOutlet UILabel *presentationLab;

@end

@implementation SetCommunicationPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupchildView];
    [self setupNav];
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;

}

- (void)setupNav {
    self.navigationItem.title = @"通讯密码";
//    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    UIButton *left = [ALSBarButton backButtonWithTitle:@"设置"];
//    [[UIButton alloc]init];
//    left.titleEdgeInsets = UIEdgeInsetsMake(0, -6, 0, 0);
//    left.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
//    [left setImage:[UIImage imageNamed:@"ARROW---LEFT"] forState:(UIControlStateNormal)];
//    [left setImage:[UIImage imageNamed:@"ARROW---LEFT-in"] forState:(UIControlStateHighlighted)];
//    [left setTitle:@"设置" forState:(UIControlStateNormal)];
//    [left setTitleColor:[UIColor colorWithRed:0.000 green:0.945 blue:0.941 alpha:1.00] forState:(UIControlStateHighlighted)];
//    [left sizeToFit];
    
    [left addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:left];
}

- (void)setupchildView {
    self.presentationLab.hidden = YES;
    
    self.ascertainBut.layer.cornerRadius = 6;
    self.ascertainBut.layer.masksToBounds = YES;
    
    self.cancelCommunicationPasswordBtn.layer.cornerRadius = 6;
    self.cancelCommunicationPasswordBtn.layer.masksToBounds = YES;
    
    self.communicationPasswordTextField.delegate = self;
    
    self.communicationPasswordTextField.tintColor = kALSHightBlueColor;

    [self textFieldShouldEndEditing:self.communicationPasswordTextField];
    
}

- (BOOL)checkingCommunicationPassword {
    
    if (self.communicationPasswordTextField.text.length == 0) {

        self.presentationLab.hidden = YES;
        return NO;
    }
    
    NSPredicate * predicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", @"^[0-9]{6}$"];
    if ([predicate evaluateWithObject:self.communicationPasswordTextField.text]) {
        //匹配上
        self.presentationLab.hidden = YES;
        return YES;
    }
    self.presentationLab.text = @"通讯密码为6位数字";
    self.presentationLab.hidden = NO;
    return NO;
}

- (IBAction)ascertainBut:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self checkingCommunicationPassword]) {
        
        [NSUD setObject:self.communicationPasswordTextField.text forKey:ALSCommunicationPassWord];
        
        __weak SetCommunicationPasswordViewController *weakself = self;
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"设置成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [weakself.navigationController popViewControllerAnimated:YES];
        }];
        
        [alertController addAction:alertAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    
}
- (IBAction)cancelCommunicationPassword:(UIButton *)sender {
    
    [NSUD setObject:nil forKey:ALSCommunicationPassWord];

    __weak SetCommunicationPasswordViewController *weakself = self;
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"取消通讯密码成功" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
    
    [alertController addAction:alertAction];
    
    [self presentViewController:alertController animated:YES completion:nil];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor= kALSHightBlueColor.CGColor;
    textField.layer.borderWidth= 1.0f;
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [self checkingCommunicationPassword];
    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor= kColorFromRGB(0xa0a0a0).CGColor;
    textField.layer.borderWidth= 1.0f;
    return YES;
}

@end
